package com.hzw.net.utils.recyclerview

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.hzw.core.resources.appDimen
import com.hzw.core.resources.appDimenPxSize

/**
 * Copyright (c) 2019, 四川绿源集科技有限公司 All rights reserved.
 * author：HuaZhongWei
 * date：2019/4/16
 * description：  recyclerView的GridLayout布局时item间距
 */

class CommonGridLayoutItemDecoration(
    private val spanCount: Int,
    //顶部
    private val top: Int = 0,
    //底部
    private val bottom: Int = 0,
    //顶部
    private val start: Int = 0,
    //底部
    private val end: Int = 0,
    //垂直间隔
    private val spacingV: Int = 0,
    //横向间隔
    private val spacingH: Int = 0,
    //是否为dimen资源
    private val isDimenRes: Boolean = true
) : ItemDecoration() {

    private val topValue: Int = getDimen(top)
    private val bottomValue: Int = getDimen(bottom)
    private val startValue: Int = getDimen(start)
    private val endValue: Int = getDimen(end)
    private val spacingVValue: Int = getDimen(spacingV)
    private val spacingHValue: Int = getDimen(spacingH)

    private fun getDimen(dimen: Int): Int {
        return if (isDimenRes) {
            if (dimen == 0) {
                0
            } else {
                appDimenPxSize(dimen)
            }
        } else {
            dimen
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        //这里是关键，需要根据你有几列来判断
        val position = parent.getChildAdapterPosition(view) // item position
        val column = position % spanCount // item column
        val itemCount = parent.adapter!!.itemCount
        val lastRowCount = if (itemCount % spanCount == 0) spanCount else itemCount % spanCount

        if ((parent.layoutManager as GridLayoutManager).orientation == LinearLayoutManager.VERTICAL) { // 纵向
            when (column) {
                0 -> {  //最左
                    outRect.left = startValue
                    outRect.right = (column + 1) * spacingHValue / spanCount
                }
                spanCount - 1 -> { //最右
                    outRect.right = endValue
                    outRect.left = spacingHValue - column * spacingHValue / spanCount
                }
                else -> {
                    outRect.left =
                        spacingHValue - column * spacingHValue / spanCount // spacing - column * ((1f / spanCount) * spacing)
                    outRect.right =
                        (column + 1) * spacingHValue / spanCount // (column + 1) * ((1f / spanCount) * spacing)
                }
            }

            if (itemCount <= spanCount) { //只有一行
                outRect.top = topValue
                outRect.bottom = bottomValue
            } else {
                when {
                    position < spanCount -> { // 最上
                        outRect.top = topValue
                        outRect.bottom = spacingVValue
                    }
                    position >= itemCount - lastRowCount -> { //最下
                        outRect.bottom = bottomValue
                    }
                    else -> {
                        outRect.bottom = spacingVValue
                    }
                }
            }
        } else { //横向
            when (column) {
                0 -> {  //最左
                    outRect.top = topValue
                    outRect.bottom = (column + 1) * spacingVValue / spanCount
                }
                spanCount - 1 -> { //最右
                    outRect.bottom = bottomValue
                    outRect.top = spacingVValue - column * spacingVValue / spanCount
                }
                else -> {
                    outRect.top =
                        spacingVValue - column * spacingVValue / spanCount // spacing - column * ((1f / spanCount) * spacing)
                    outRect.bottom =
                        (column + 1) * spacingVValue / spanCount // (column + 1) * ((1f / spanCount) * spacing)
                }
            }

            if (itemCount <= spanCount) { //只有一行
                outRect.left = startValue
                outRect.right = endValue
            } else {
                when {
                    position < spanCount -> { // 最上
                        outRect.left = startValue
                        outRect.right = spacingHValue
                    }
                    position >= itemCount - lastRowCount -> { //最下
                        outRect.right = endValue
                    }
                    else -> {
                        outRect.right = spacingHValue
                    }
                }
            }
        }
    }
}